package com.ias.log.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import jakarta.servlet.http.HttpServletRequest;

@Slf4j
public class LogUtil {

    /**
     * 获取path
     * @return
     */
    public static String getPath(){
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes())
                .getRequest();
        return request.getRequestURI();
    }
    /**
     * 获取head
     * @return
     */
    public static String getHead(String key){
        return ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes())
                .getRequest().getHeader(key);

    }
    /**
     * 获取param
     * @return
     */
    public static String getParam(String key){
        return ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes())
                .getRequest().getParameter(key);

    }
    /**
     * 获取请求类型
     * @return
     */
    public static  Integer getServiceType(){
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes())
                .getRequest();
        String serviceId = request.getHeader("serviceType");
        if(StringUtils.hasText(serviceId)){
            try {
                return Integer.parseInt(serviceId);
            } catch (NumberFormatException ignored) {
                log.error(ExcpUtil.getSubStackTrace(ignored));
            }
        }
        return 2;
    }
}
